package com.ideviu.webapp.action;

import java.io.Serializable;

import com.ideviu.model.TipoNegocio;
import com.ideviu.webapp.action.BasePage;
import com.ideviu.service.GenericManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component("tipoNegocioForm")
@Scope("request")
public class TipoNegocioForm extends BasePage implements Serializable {
    private GenericManager<TipoNegocio, Long> tipoNegocioManager;
    private TipoNegocio tipoNegocio = new TipoNegocio();
    private Long id;

    @Autowired
    public void setTipoNegocioManager(@Qualifier("tipoNegocioManager") GenericManager<TipoNegocio, Long> tipoNegocioManager) {
        this.tipoNegocioManager = tipoNegocioManager;
    }

    public TipoNegocio getTipoNegocio() {
        return tipoNegocio;
    }

    public void setTipoNegocio(TipoNegocio tipoNegocio) {
        this.tipoNegocio = tipoNegocio;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        tipoNegocioManager.remove(tipoNegocio.getId());
        addMessage("tipoNegocio.deleted");

        return "list";
    }

    public String edit() {
        // Workaround for not being able to set the id using #{param.id} when using Spring-configured managed-beans
        if (id == null) {
            id = new Long(getParameter("id"));
        }
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            tipoNegocio = tipoNegocioManager.get(id);
        } else {
            tipoNegocio = new TipoNegocio();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (tipoNegocio.getId() == null || tipoNegocio.getId() == 0);
        tipoNegocioManager.save(tipoNegocio);

        String key = (isNew) ? "tipoNegocio.added" : "tipoNegocio.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
            return "edit";
        }
    }
} 